#include <stdio.h>
#include <stdlib.h>
#include "GDLR/mathematics.h"

void ExceptionHandler()
{
    printf("exception!!!\n");
    system("pause");
}

int main() {
    struct vector x[4];
    x[0].dim = x[1].dim = x[2].dim = x[3].dim = 3;
    for (int i = 0; i < 4; i++) {
        x[i].val = (double *)malloc(sizeof(double) * 3);
    }
    x[0].val[0] = 2;
    x[0].val[1] = 2;
    x[0].val[2] = 3;
    
    x[1].val[0] = 3;
    x[1].val[1] = 3;
    x[1].val[2] = 4;
    
    x[2].val[0] = 4;
    x[2].val[1] = 4;
    x[2].val[2] = 5;
    
    x[3].val[0] = 5;
    x[3].val[1] = 5;
    x[3].val[2] = 6;

    double y[4] = {1, 2, 3, 4};

    struct vector theta_o;
    theta_o.dim = 3;
    theta_o.val = (double*)malloc(3*sizeof(double));
    theta_o.val[0] = 100;
    theta_o.val[1] = 3;
    theta_o.val[2] = -3;

    double epsilon_o = 0.0;

    int iter;
    double loss;
    struct vector theta;
    double epsilon;

    printf("---debug---\n");

    LinearRegression(x, y, 4, &theta_o, &epsilon_o, 0.02, 0.8, 10000, 10, 100, 1e-6, &iter, &loss, &theta, &epsilon);

    printf("---result---\n");
    printf("iter : %d\nloss : %lf\nepsilon : %lf\n theta :\n", iter, loss, epsilon);
    for (int i = 0; i < 3; i++) {
        printf("    theta_%d = %lf\n", i, theta.val[i]);
    }

    free(theta.val);
    free(theta_o.val);
    for (int i = 0; i < 4; i++) {
        free(x[i].val);
    }

    return 0;
}